﻿using ApplLogic.ApplClasses;
using Entities;

namespace ApplLogic.DbToApplConvertor
{
    public static class DbApplObjectConvertor
    {

        #region DbObjectsToApplObjects

        public static ApplWorkerStatInWeek CreateApplWorkerStatFromDbWorkerStat(WorkerStatInAWeek workerStat)
        {
            ApplWorkerStatInWeek applWorkerStatInWeek = new ApplWorkerStatInWeek
                                                            {
                                                                ShiftsThatAskedAndGot =
                                                                    workerStat.ShiftsThatAskedAndNotGet.Value,
                                                                ShiftsThatAskedNotToBeAndGot =
                                                                    workerStat.ShiftsThatAskedNotToBeAndGet.Value,
                                                                ShiftsThatNotAskedAndGot =
                                                                    workerStat.ShiftsThatNotAskedAndGet.Value,
                                                                TotalForbiddenShifts = workerStat.ForbiddenShifts.Value,
                                                                TotalWantedShifts = workerStat.WantedShifts.Value,
                                                                TotalNumberOfShiftsGot = workerStat.TotalShifts.Value,
                                                                WeekStartTime = workerStat.WeekStartTime,
                                                                WeekSEndTime = workerStat.WeekEndTime,
                                                                WorkerRef =
                                                                    CreateApplWorkerFromDbWorker(workerStat.Worker)

                                                            };
            return applWorkerStatInWeek.DeepClone();
        }
        public static ApplWorker CreateApplWorkerFromDbWorker(Worker sourceWorker)
        {
            ApplWorker applWorker = new ApplWorker
                                        {
                                            PersonalDetails =
                                                {
                                                    Id = sourceWorker.WorkerID,
                                                    FirstName = sourceWorker.Name,
                                                    LastName = sourceWorker.LastName,
                                                    Password = sourceWorker.Password,
                                                    UserName = sourceWorker.UserName,
                                                    BirthDate = sourceWorker.BirthDate
                                                },
                                            MaxShiftsThatCanGive = sourceWorker.MaxNumberOfShifts,
                                            StartOfWork = sourceWorker.StartOfWork
                                        };
            applWorker.PersonalDetails.EmailAdress = sourceWorker.EmailAdress;
            return applWorker.DeepClone();
        }

        public static ApplShift CreateApplShiftFromDbShift(Shift sourceShift)
        {
            ApplShift applShift = new ApplShift
                                      {
                                          Id = sourceShift.ShiftID,
                                          Name = sourceShift.Name,
                                          NumberOfWorkersNeeded = sourceShift.NumberOfWorkersNeeded,
                                          BeginTime = sourceShift.BeginTime,
                                          EndTime = sourceShift.EndTime
                                      };
            return applShift.DeepClone();
        }

        public static ApplSingleRequest CreateApplSingleRequestFromDbSingleRequest(Request sourceRequest)
        {
            ApplSingleRequest applRequest = new ApplSingleRequest
                                                {
                                                    ApplWorker = CreateApplWorkerFromDbWorker(sourceRequest.Worker),
                                                    ApplShift = CreateApplShiftFromDbShift(sourceRequest.Shift),
                                                    Type = ApplSingleRequest.ConvertStringToEnum(sourceRequest.Type)
                                                };
            return applRequest.DeepClone();
        }

        public static ApplSingleResult CreateApplSingleRequestFromDbSingleRequest(Result sourceResult)
        {
            ApplSingleResult applResult = new ApplSingleResult();
            applResult.ApplWorker = CreateApplWorkerFromDbWorker(sourceResult.Worker);
            applResult.ApplShift = CreateApplShiftFromDbShift(sourceResult.Shift);
            applResult.Step = ApplSingleResult.ConvertStringToEnum(sourceResult.Step);
            return applResult.DeepClone();
        }

        public static ApplManager CreateApplManagerFromDbManager(Manager sourceManager)
        {
            ApplManager applManager = new ApplManager();
            applManager.PersonalDetails.EmailAdress = sourceManager.EmailAdress;
            applManager.PersonalDetails.FirstName = sourceManager.FirstName;
            applManager.PersonalDetails.Id = sourceManager.ManagerID;
            applManager.PersonalDetails.LastName = sourceManager.LastName;
            applManager.PersonalDetails.Password = sourceManager.Password;
            applManager.PersonalDetails.UserName = sourceManager.UserName;
            applManager.PersonalDetails.BirthDate = sourceManager.BirthDate;
            return applManager.DeepClone();
        }
        #endregion

        #region ApplObjectsToDbObjects

        public static WorkerStatInAWeek CreateDbWorkerStatInAWeekFromApplWorkerStatInAWeek(ApplWorkerStatInWeek applWorkerStatInWeek)
        {
            WorkerStatInAWeek workerStatInAWeek = new WorkerStatInAWeek();
            workerStatInAWeek.TotalShifts = applWorkerStatInWeek.TotalNumberOfShiftsGot;
            workerStatInAWeek.WeekEndTime = applWorkerStatInWeek.WeekSEndTime;
            workerStatInAWeek.WeekStartTime = applWorkerStatInWeek.WeekStartTime;
           // workerStatInAWeek.Worker = CreateDbWorkerFromApplWorker(applWorkerStatInWeek.WorkerRef);
            workerStatInAWeek.WorkerID = applWorkerStatInWeek.WorkerRef.PersonalDetails.Id;
            workerStatInAWeek.ShiftsThatAskedAndNotGet = applWorkerStatInWeek.ShiftsThatAskedAndGot;
            workerStatInAWeek.ShiftsThatNotAskedAndGet = applWorkerStatInWeek.ShiftsThatNotAskedAndGot;
            workerStatInAWeek.ShiftsThatAskedNotToBeAndGet = applWorkerStatInWeek.ShiftsThatAskedNotToBeAndGot;
            workerStatInAWeek.WantedShifts = applWorkerStatInWeek.TotalWantedShifts;
            workerStatInAWeek.ForbiddenShifts = applWorkerStatInWeek.TotalForbiddenShifts;
            return workerStatInAWeek;
        }
        public static Worker CreateDbWorkerFromApplWorker(ApplWorker applWorker)
        {
            Worker worker = new Worker();
            worker.Password = applWorker.PersonalDetails.Password;
            worker.UserName = applWorker.PersonalDetails.UserName;
            worker.Name = applWorker.PersonalDetails.FirstName;
            worker.LastName = applWorker.PersonalDetails.LastName;
            worker.MaxNumberOfShifts = applWorker.MaxShiftsThatCanGive;
            worker.StartOfWork = applWorker.StartOfWork;
            worker.WorkerID = applWorker.PersonalDetails.Id;
            worker.BirthDate = applWorker.PersonalDetails.BirthDate;
            worker.EmailAdress = applWorker.PersonalDetails.EmailAdress;
            return worker;
        }

        public static Shift CreateShiftFromApplShift(ApplShift applShift)
        {
            Shift shift = new Shift();
            shift.Name = applShift.Name;
            shift.NumberOfWorkersNeeded = applShift.NumberOfWorkersNeeded;
            shift.ShiftID = applShift.Id;
            shift.BeginTime = applShift.BeginTime;
            shift.EndTime = applShift.EndTime;
            return shift;
        }

        public static Manager CreateManagerFromApplManager(ApplManager applManager)
        {
            Manager manager = new Manager();
            manager.BirthDate = applManager.PersonalDetails.BirthDate;
            manager.EmailAdress = applManager.PersonalDetails.EmailAdress;
            manager.FirstName = applManager.PersonalDetails.FirstName;
            manager.LastName = applManager.PersonalDetails.LastName;
            manager.ManagerID = applManager.PersonalDetails.Id;
            manager.Password = applManager.PersonalDetails.Password;
            manager.UserName = applManager.PersonalDetails.UserName;
            return manager;
        }

        public static Request CreateDbRequestFromApplRequest(ApplSingleRequest applRequest)
        {
            Request dbRequest = new Request();
            dbRequest.Worker = CreateDbWorkerFromApplWorker(applRequest.ApplWorker);
            dbRequest.Shift = CreateShiftFromApplShift(applRequest.ApplShift);
            dbRequest.Type = ApplSingleRequest.ConvertEnumTypeToString(applRequest.Type);
            dbRequest.WorkerID = applRequest.ApplWorker.PersonalDetails.Id;
            dbRequest.ShiftID = applRequest.ApplShift.Id;
            return dbRequest;

        }
        #endregion
    }
}
